<template>
  <el-dialog
    :title="!dataForm.informationId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="资讯标题" prop="informationTitle">
      <el-input v-model="dataForm.informationTitle" placeholder="资讯标题"></el-input>
    </el-form-item>
    <el-form-item label="资讯内容" prop="informationContent">
      <el-input v-model="dataForm.informationContent" placeholder="资讯内容"></el-input>
    </el-form-item>
    <el-form-item label="管理员id" prop="managerId">
      <el-input v-model="dataForm.managerId" placeholder="管理员id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="informationCreatedate">
      <el-input v-model="dataForm.informationCreatedate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="房源id" prop="resourceId">
      <el-input v-model="dataForm.resourceId" placeholder="房源id"></el-input>
    </el-form-item>
    <el-form-item label="房源默认图片" prop="defauleImg">
      <el-input v-model="dataForm.defauleImg" placeholder="房源默认图片"></el-input>
    </el-form-item>
    <el-form-item label="房源名" prop="resourceName">
      <el-input v-model="dataForm.resourceName" placeholder="房源名"></el-input>
    </el-form-item>
    <el-form-item label="是否发布0不发布1发布" prop="resourcePublishStatus">
      <el-input v-model="dataForm.resourcePublishStatus" placeholder="是否发布0不发布1发布"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          informationId: 0,
          informationTitle: '',
          informationContent: '',
          managerId: '',
          informationCreatedate: '',
          resourceId: '',
          defauleImg: '',
          resourceName: '',
          resourcePublishStatus: ''
        },
        dataRule: {
          informationTitle: [
            { required: true, message: '资讯标题不能为空', trigger: 'blur' }
          ],
          informationContent: [
            { required: true, message: '资讯内容不能为空', trigger: 'blur' }
          ],
          managerId: [
            { required: true, message: '管理员id不能为空', trigger: 'blur' }
          ],
          informationCreatedate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          resourceId: [
            { required: true, message: '房源id不能为空', trigger: 'blur' }
          ],
          defauleImg: [
            { required: true, message: '房源默认图片不能为空', trigger: 'blur' }
          ],
          resourceName: [
            { required: true, message: '房源名不能为空', trigger: 'blur' }
          ],
          resourcePublishStatus: [
            { required: true, message: '是否发布0不发布1发布不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.informationId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.informationId) {
            this.$http({
              url: this.$http.adornUrl(`/admin/information/info/${this.dataForm.informationId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.informationTitle = data.information.informationTitle
                this.dataForm.informationContent = data.information.informationContent
                this.dataForm.managerId = data.information.managerId
                this.dataForm.informationCreatedate = data.information.informationCreatedate
                this.dataForm.resourceId = data.information.resourceId
                this.dataForm.defauleImg = data.information.defauleImg
                this.dataForm.resourceName = data.information.resourceName
                this.dataForm.resourcePublishStatus = data.information.resourcePublishStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/information/${!this.dataForm.informationId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'informationId': this.dataForm.informationId || undefined,
                'informationTitle': this.dataForm.informationTitle,
                'informationContent': this.dataForm.informationContent,
                'managerId': this.dataForm.managerId,
                'informationCreatedate': this.dataForm.informationCreatedate,
                'resourceId': this.dataForm.resourceId,
                'defauleImg': this.dataForm.defauleImg,
                'resourceName': this.dataForm.resourceName,
                'resourcePublishStatus': this.dataForm.resourcePublishStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
